#if !defined __WINAMPFRAME_H
#define __WINAMPFRAME_H

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "FlatTabCtrl.h"
#include "ExListViewCtrl.h"
#include "../client/TimerManager.h"

#define WINAMP_MESSAGE_MAP 13

class WinampFrame : public MDITabChildWindowImpl<WinampFrame>, public StaticFrame<WinampFrame, ResourceManager::MENU_WINAMP>
	, private TimerManagerListener
{
public:
	typedef MDITabChildWindowImpl<WinampFrame> baseClass;

	WinampFrame() : trackBarContainer(_T("edit"), this, WINAMP_MESSAGE_MAP),
		hwndWinamp(FindWindow(_T("Winamp v1.x"), NULL)), 
		closed(false), oldPos(0), dragging(false)
	{ 
		TimerManager::getInstance()->addListener(this);
	}
	virtual ~WinampFrame() { }

	DECLARE_FRAME_WND_CLASS_EX(_T("WinampFrame"), IDR_WINAMP, 0, COLOR_3DFACE);

	BEGIN_MSG_MAP(WinampFrame)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		MESSAGE_HANDLER(WM_CLOSE, onClose)
		COMMAND_ID_HANDLER(IDC_PLAY, onButton)
		COMMAND_ID_HANDLER(IDC_STOP, onButton)
		COMMAND_ID_HANDLER(IDC_PAUSE, onButton)
		COMMAND_ID_HANDLER(IDC_REFRESH_WINAMP, onButton)
		NOTIFY_HANDLER(IDC_WINAMP_LIST, NM_DBLCLK, onDblClick)
		NOTIFY_HANDLER(IDC_WINAMP_LIST, NM_CUSTOMDRAW, onCustomDraw)
		NOTIFY_HANDLER(IDC_POSITION, NM_RELEASEDCAPTURE, onTrackBarRelease)
		CHAIN_MSG_MAP(baseClass)
	ALT_MSG_MAP(WINAMP_MESSAGE_MAP)
		MESSAGE_HANDLER(WM_LBUTTONDOWN, onLBtnDown)
		MESSAGE_HANDLER(WM_LBUTTONUP, onLBtnUp)
	END_MSG_MAP()

	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled);
	LRESULT onClose(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled);
	LRESULT onButton(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
	LRESULT onChar(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) { return 0; };
	LRESULT onDblClick(int /*idCtrl*/, LPNMHDR /* pnmh */, BOOL& bHandled);
	LRESULT onCustomDraw(int /*idCtrl*/, LPNMHDR pnmh, BOOL& bHandled);
	LRESULT onTrackBarRelease(int /*idCtrl*/, LPNMHDR /* pnmh */, BOOL& bHandled);
	LRESULT onLBtnDown(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled) {
		bHandled = FALSE;
		dragging = true;
		return 0;
	}
	LRESULT onLBtnUp(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled) {
		bHandled = FALSE;
		dragging = false;
		return 0;
	}
	void UpdateLayout(BOOL bResizeBars = TRUE);
	void loadAll();
	void setStatus();

	virtual void OnFinalMessage(HWND /*hWnd*/) {
		delete this;
	}
	// TimerManagerListener
	virtual void on(TimerManagerListener::Second, DWORD /*aTick*/) throw();
	
private:

	bool closed;
	bool dragging;
	int oldPos;

	HWND hwndWinamp;
	CButton ctrlPlay;
	CButton ctrlStop;
	CButton ctrlPause;
	CButton ctrlRefresh;
	ExListViewCtrl ctrlList;
	CStatusBarCtrl ctrlStatus;
	CTrackBarCtrl ctrlPosition;
	CContainedWindow trackBarContainer;

	void refresh();
	void loadm3u();
};

#endif // __WINAMPFRAME_H